﻿using System;
using System.Linq;

namespace PKTools.Database
{
    public class SqlObject
    {
        public virtual void __Reset()
        {
        }

        public virtual void __Initialize()
        {
        }

        public static T? ToNullable<T>(object value) where T : struct
        {
            return value == DBNull.Value ? null : new T?((T)value);
        }

        public static T ToObject<T>(object value) where T : class
        {
            return value == DBNull.Value ? null : value as T;
        }
    }

    public class Spyed : SqlObject
    {
        protected bool[]
            __changed;
        protected string[]
            __fields;

        public string[] __GetChanged()
        {
            return this.__fields.Where((e, i) => this.__changed[i]).ToArray();
        }

        public override void __Reset()
        {
            for (int i = 0; i < this.__changed.Length; i++)
                this.__changed[i] = false;
        }
    }
}
